package com.example.demo.materialPurchase.controller;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.ResponseData;
import com.example.demo.entity.Department;
import com.example.demo.entryWarehouse.entity.BaseInformation;
import com.example.demo.entryWarehouse.mapper.BaseInformationMapper;
import com.example.demo.materialPurchase.entity.MaterialPurchase;
import com.example.demo.materialPurchase.entity.PurchaseInformation;
import com.example.demo.materialPurchase.mapper.MaterialPurchaseMapper;
import com.example.demo.materialPurchase.mapper.PurchaseInformationMapper;
import com.example.demo.materialPurchase.service.PurchaseInformationService;
import com.example.demo.materialPurchase.service.impl.MaterialPurchaseServiceImpl;
import com.example.demo.materialPurchase.service.impl.PurchaseInformationServiceImpl;
import com.example.demo.systemManagement.service.PurchaseInfoManageService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lby
 * @since 2021-05-09
 */
@RestController
@RequestMapping("/materialPurchase/purchase-information")
@PreAuthorize("hasAnyAuthority('4','1','2')")
@CrossOrigin(origins = {"*"})
public class PurchaseInformationController {

	@Autowired
	private PurchaseInformationMapper purchaseInformationMapper;
	
	@Autowired
	private MaterialPurchaseMapper materialMapper;
	@Autowired
	private MaterialPurchaseServiceImpl materialservice;
	
	/**
	 * 插入一条新的采购信息,并将计划状态设置为 计划中
	 * @param materialName
	 * @param purchaseNum
	 * @param partUsed
	 * @param remark
	 * @param departName
	 * @return
	 */
	@PostMapping("/insOne")
	public ResponseData insertOne(@RequestParam("materialName") String materialName,
			@RequestParam("purchaseNum") int purchaseNum,@RequestParam("partUsed") String partUsed,
			@RequestParam("remark") String remark,@RequestParam("departName") String departName,
			@RequestParam("preStatus") String preStatus) {
		
		// 获取当前时间
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM");
		Date date = new Date(System.currentTimeMillis());
		String dateString = formatter.format(date);
//		System.out.println(dateString)
		
		// 获取部门id
		Department dep = new Department();
		dep = materialMapper.seldepId(departName);
//		System.out.println("dep:"+dep.getId().toString());
		
		// 判断 PurchaseInformation表中 是否有 表id
		QueryWrapper<PurchaseInformation> wrapper = new QueryWrapper<PurchaseInformation>();
		PurchaseInformation p = new PurchaseInformation();
		wrapper.eq("table_id", dateString + dep.getId().toString());
//		System.out.println(dateString + dep.getId().toString());
		p= purchaseInformationMapper.selectOne(wrapper);
//		System.out.println(p);
		
		if(p != null) { // 判断若有该表数据则直接插入
			
			String tableId = p.getTableId();
			return materialservice.insNewOne(tableId, materialName, departName, purchaseNum, partUsed, preStatus, remark);
		
		}else { // 否则，新建表 然后再 插入新字段
			
			PurchaseInformation newpur = new PurchaseInformation();
			newpur.setTableId(dateString + dep.getId().toString());
			purchaseInformationMapper.insert(newpur);
			 
			return materialservice.insNewOne(newpur.getTableId(), materialName, departName, purchaseNum, partUsed, preStatus, remark);
			
		}
	}
}

